# 多阶段构建 - 构建阶段
FROM golang:1.24.3-alpine AS builder

# 设置工作目录
WORKDIR /app

# 安装必要的系统依赖
RUN apk add --no-cache git ca-certificates tzdata

# 复制go mod文件
COPY go.mod go.sum ./

# 下载依赖
RUN go mod download

# 复制源代码
COPY . .

# 构建应用
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o jobs main.go

# 运行阶段
FROM alpine:latest

# 安装必要的系统工具
RUN apk --no-cache add ca-certificates tzdata curl

# 创建非root用户
RUN addgroup -g 1001 -S appgroup && \
    adduser -u 1001 -S appuser -G appgroup

# 设置工作目录
WORKDIR /app

# 从构建阶段复制二进制文件
COPY --from=builder /app/jobs .

# 复制配置文件
COPY --from=builder /app/config ./config

# 创建必要的目录
RUN mkdir -p runtime/logs runtime/jobs && \
    chown -R appuser:appgroup /app

# 切换到非root用户
USER appuser

# 暴露端口
EXPOSE 36363

# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://127.0.0.1:36363/jobs/health || exit 1

# 设置环境变量
ENV GIN_MODE=release
ENV TZ=Asia/Shanghai

# 启动命令
ENTRYPOINT ["./jobs", "start"] 